﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class JLTNVKDOC : DocumentBase
    {
        
        #region=============Properties=======================
        string Mandant = "00";
        ApplicationBase ApplicationBase;
        bool IsSetUp = false;
        public OracleConnection OracleDB;
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        JLTNVKDOCData m_Data;
        INTEXDBC INTEXDBC = null;
        public DataSet setResult;
        JLTNVKDOCReport m_rpt;
        string UserID = "";
        #region==========CollectionViewSource=================
        SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)this.FindResource("SetData");
            }
        }
        SETINTEXDB SetQP
        {
            get
            {
                return (SETINTEXDB)this.FindResource("SetQP");
            }
        }

        CollectionViewSource vwVKPOS
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwVKPOS");
            }
        }
        DataRow dwVKPOS
        {
            get
            {
                if (vwVKPOS.View == null)
                    return null;
                if (vwVKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwVKPOS.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwVKKOPF
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwVKKOPF");
            }
        } 
        string art
        {
            get
            {
                if (dwVKPOS == null)
                    return "";
                return dwVKPOS["VKPART"].ToString();

            }
        }
        #endregion
        int total = 0;
        public JLTNVKDOC()
        {
            InitializeComponent();
            this.Name = "销售合同";
            ZoneAccount.Visibility = Visibility.Hidden;
            ZoneAuditing.Visibility = Visibility.Hidden;
            ZoneOrder.Visibility = Visibility.Hidden;
        }
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (!IsSetUp)
            {
                ApplicationBase.Quit();
            }
            base.OnClosing(e); 
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsSetUp)
                {
                    ApplicationBase = new ApplicationBase();
                    ApplicationBase.Load();
                    dicParamater = ApplicationBase.dicParamater;
                    #region============TESTINGDATA================
                    txtUserID.Text = ApplicationBase.User;
                    txtPassword.Password = ApplicationBase.Pwd;
                    #endregion
                }
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                Mandant = (string)dicParamater["MANDANT"];

                m_Data = new JLTNVKDOCData(OracleDB, proxy);
                INTEXDBC = new INTEX.DataBase.Tools.INTEXDBC(OracleDB, proxy);
                string memberv9 = m_Data.GetMemberV9("JLT");
                string Admn = m_Data.GetAdministration("JLT");
                MemberV9.Text = memberv9;
                Administration.Text = Admn;

                #region============TESTINGDATA================
                txtVKPAUFKRS.Text = "C0";
                txtVKPAUFLFD.Text = "100227";
                txtVKPAUFKRS.Text = "S3";
                txtVKPAUFLFD.Text = "002001";

                txtUserID.Text = (string)dicParamater["User"];
                txtPassword.Password = (string)dicParamater["Pwd"];
                #endregion
                btnReport.IsEnabled = true;
                btnPrintPreview.IsEnabled = true;
                btnGetVKPOS.IsEnabled = false;
                SetQP.Merge(m_Data.LoadQPPLAN());
                vwVKPOS.View.CurrentChanged += vwVKPOS_CurrentChanged;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }

        void vwVKPOS_CurrentChanged(object sender, EventArgs e)
        {
            if (art == "")
                return;
            try
            {
                SetData.Merge(m_Data.GetQPStep(art)); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                IsSetUp = true;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    InitializeComponent();
                    this.dicParamater = dicParamater;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            ZoneAccount.Visibility = Visibility.Hidden;
            ZoneAuditing.Visibility = Visibility.Hidden;
            ZoneOrder.Visibility = Visibility.Hidden;
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = m_Data.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            txtLogIn.Background = new SolidColorBrush(Colors.Yellow);
            DataSet setResult = m_Data.LogIn(Mandant, userID, SPassword);
            if (setResult.Tables.Count == 0)
                return;
            if (setResult.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("请检查登陆信息");
                return;
            }
            DataRow dwUser = setResult.Tables[0].Rows[0];
            txtSPRACHKZ.Text = dwUser["HRSPRKZ"].ToString();
            UserID = userID;
            m_Data.UserID = userID;
            this.UserID = userID;

            m_Data.WorkingDate = DateTime.Now.Year.ToString() + m_Data.Right("00" + DateTime.Now.Month.ToString(), 2)
                + m_Data.Right("00" + DateTime.Now.Day.ToString(), 2);
            m_Data.MANDANT = Mandant;
            m_Data.SPRACHKZ = txtSPRACHKZ.Text.Trim();
            expLog.IsExpanded = false;
            //expLog.Visibility = Visibility.Hidden;
            btnReport.IsEnabled = true;
            btnPrintPreview.IsEnabled = true;
            btnGetVKPOS.IsEnabled = true;
            btnDistribute.IsEnabled = false;
            IsBelongs2V9.IsChecked = false;
            IsOnAuditing.IsChecked = false;
            IsSendForAuditing.IsChecked = true;
            ZoneOrder.Visibility = Visibility.Visible;
            ZoneAccount.Visibility = Visibility.Visible;
            if (MemberV9.Text.Contains(userID.Trim() + ";"))
            {
                btnDistribute.IsEnabled = true;
                IsBelongs2V9.IsChecked = true;
                IsOnAuditing.IsChecked = true;
                IsSendForAuditing.IsChecked = false;
                this.txtLogIn.Text = DateTime.Now.ToLocalTime().ToString() + ": " + userID + "开始审单";
                ZoneAccount.Visibility = Visibility.Hidden;
                ZoneAuditing.Visibility = Visibility.Visible;
            }
            if (Administration.Text.Contains(userID.Trim() + ";"))
            {
                ZoneAccount.Visibility = Visibility.Hidden;
                ZoneAuditing.Visibility = Visibility.Hidden;
                ZoneOrder.Visibility = Visibility.Hidden;
            }

            EventDispatcher.Instance.RaiseStatus(txtLogIn.Text, StatusEventType.UserLogIn);
        }


        #endregion
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string PrintType = "JustExcel";
                GenerateReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }
        private void btnDistribute_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType, true);
        }
        public void GenerateReport(string PrintType, bool isapproved = false)
        {
            try
            {
                JLTNVKDOCReport m_rpt = new JLTNVKDOCReport(dicParamater);
                JLTNVKDOCData m_DX = new JLTNVKDOCData(dicParamater);                
                m_rpt.dsSource = SetData;
                m_rpt.PrintType = PrintType;
                m_rpt.exAgent = m_DX.exAgent;
                m_rpt.OracleDB = this.OracleDB;
                m_rpt.proxy = this.proxy;
                m_rpt.UserID = this.txtUserID.Text;
                m_rpt.WriteExcelReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
  
        private void btnGetVKPOS_Click(object sender, RoutedEventArgs e)
        {
            EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
            try
            {
                SetData.Clear();
                DataSet setResult = new DataSet();
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataRow dwVKKOPF = SetData.VKKOPF.NewVKKOPFRow();
                    dwVKKOPF["VKKMANDANT"] = "00";
                    dwVKKOPF["VKKAUFKRS"] = m_Data.Left(txtVKPAUFKRS.Text + "  ", 2);
                    dwVKKOPF["VKKAUFLFD"] = m_Data.Right("0000000" + txtVKPAUFLFD.Text, 6);
                    setResult = m_Data.GetData(dwVKKOPF);
                    SetData.Merge(setResult);
                    if (vwVKPOS.View != null)
                    {
                        vwVKPOS.View.MoveCurrentToFirst();
                    }
                }
                btnReport.IsEnabled = true;
                btnPrintPreview.IsEnabled = true;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            EventDispatcher.Instance.RaiseStatus("查询结束", StatusEventType.StopProgress);
        }

     
    }
}
